package com.yidu.hbats.action.emp;

import com.yidu.hbats.model.AtsEmpLeader;
import com.yidu.hbats.service.emp.AtsEmpLeaderService;
import com.yidu.utils.Message;
import com.yidu.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: hbats
 * @description: 中心领导控制器
 * @author: 张超
 * @create: 2019-11-01 11:18
 */

@Controller
@RequestMapping("AtsEmpLeaderController")
public class AtsEmpLeaderController {
    /**
     * 中心领导信息接口
     */
    @Resource
    private AtsEmpLeaderService atsEmpLeaderService;

    /**
     * 新增或者修改
     * @param atsEmpLeader
     * @return
     */
    @RequestMapping("insertOrUpdate")
    @ResponseBody
    public Message insertOrUpdate(AtsEmpLeader atsEmpLeader){

        if(atsEmpLeader.getLeaderId()!=null){//有ID是修改

        }else{
            atsEmpLeader.setIsva(1);//是否有效
            Date date = new Date();
            atsEmpLeader.setCreatTime(date);//创建时间
        }


        atsEmpLeaderService.saveOrUpdate(atsEmpLeader);

        Message message = new Message();
        message.setNum(1);
        message.setMsg("编辑成功");
        return message;
    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    @RequestMapping("findById")
    @ResponseBody
    public AtsEmpLeader findById(Integer id){
        return atsEmpLeaderService.findById(id);
    }


    /**
     * 查询所有  返回map
     * @param atsEmpLeader
     * @param page
     * @return
     */
    @RequestMapping("selectAll")
    @ResponseBody
    public Map selectAll(AtsEmpLeader atsEmpLeader, Page page){
        Map map = new HashMap();

        List<AtsEmpLeader> list = atsEmpLeaderService.selectAll(atsEmpLeader,page);

        int count = atsEmpLeaderService.selectCount(atsEmpLeader);
        //int count = 10;
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);
        return map;
    }


    /**
     * 按id删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Message delete(Integer id){
        Message message = new Message();
        if(id==null){
            message.setNum(0);
            message.setMsg("删除失败");
            return message;
        }else{
            AtsEmpLeader atsEmpLeader = new AtsEmpLeader();
            atsEmpLeader.setLeaderId(id);
            atsEmpLeader.setIsva(2);
            atsEmpLeaderService.saveOrUpdate(atsEmpLeader);

            message.setNum(1);
            message.setMsg("删除成功");
            return message;
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("deleteAll")
    @ResponseBody
    public Message deleteAll(String ids){
        Message message = new Message();
        if(atsEmpLeaderService.deleteAll(ids)==0){
            message.setNum(0);
            message.setMsg("删除失败");
        }else{
            message.setNum(1);
            message.setMsg("删除成功");
        }
        return message;
    }


    /**
     * 查询所有  返回list
     * @param atsEmpLeader
     * @return
     */
    @RequestMapping("findAll")
    @ResponseBody
    public List<AtsEmpLeader> findAll(AtsEmpLeader atsEmpLeader){
        return atsEmpLeaderService.findAll(atsEmpLeader);
    }

}